#include "shapes.h"

namespace physicsworks {

/**
 * Builds another axis-aligned box that bounds the transformed one.
 * 
 * It's basically a matrix-vector multiplication to transform the
 * box's vertices and with them generate the max and min vector
 * but in an optimized way.
 */
AABox& AABox::transform(const vec3& position, const mat3& orientation)
{
	vec3 pMin = min;
	vec3 pMax = max;
	
	max = min = position;
	
	for (size_t r = 0; r < 3; ++r) {
		for (size_t c = 0; c < 3; ++c) {
			// TODO: optimize this using the ori(r,c)'s bit of signal
			if (orientation(r,c) >= 0) {
				min(r) += orientation(r,c) *  pMin(c);
				max(r) += orientation(r,c) *  pMax(c);
			} else {
				min(r) += orientation(r,c) *  pMax(c);
				max(r) += orientation(r,c) *  pMin(c);
			}
		}
	}
	
	return *this;
}

}
